package com.quasar.hpatient.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.quasar.hpatient.R;

import lib.quasar.util.DimenUtil;
import lib.quasar.util.GlideUtil;

public class StateView extends FrameLayout {

    public final static int STATE_LOADING = 1;
    public final static int STATE_FAIL = 2;
    private ImageView image;

    public StateView(@NonNull Context context) {
        super(context);
    }

    public StateView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public StateView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        image = new ImageView(getContext());
        LayoutParams params = new LayoutParams(DimenUtil.dp2px(80), DimenUtil.dp2px(80));
        params.gravity = Gravity.CENTER;
        image.setLayoutParams(params);
        addView(image);
        setState(STATE_LOADING);
    }

    public ImageView getImage() {
        return image;
    }

    public void setImageResource(int imageRes) {
        if (null == image) return;
        image.setImageResource(imageRes);
    }

    public void setState(int state) {
        switch (state) {
            case STATE_LOADING:
                GlideUtil.loadImageSimple(getContext(), image, R.drawable.ic_comm_recycler_loading);
                break;
            case STATE_FAIL:
                GlideUtil.loadImageSimple(getContext(), image, R.drawable.ic_comm_recycler_fail);
                break;
        }
    }
}
